# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ///////////////////////////////////////////////////////////////
# @File    :   getImageBaseData.py
# @Time    :   2024/08/21 14:39:56
# @Author  :   kai.luo
# @Version :   1.0.0
# @Desc    :   获取医疗图片中检查项目的公共库
# ///////////////////////////////////////////////////////////////

import re
import traceback
from moudles.MedicalReportForm.commonLibrary.common import *
from moudles.MedicalReportForm.commonLibrary.getImageMainData import MainInfo


class ChongqinMainInfo(object):
    """
    项目,结果,单位,参考区间,提示
    Args:
        object (_type_): _description_
    """

    def __init__(self, **kwarg) -> None:
        # 初始化公共头部库
        self.comonMainInfo = MainInfo()

    def parserMainTitleIndex(self, data):
        """
        有效列的下标
        Args:
            data (_type_): _description_
        """
        # print(f'parserMainTitle---{data}')
        # 获取有效的标题所在X轴的排序
        display_col_index_list = self.comonMainInfo.parserMainTitleIndex(data)

        return display_col_index_list

    def parserMainContexts(self, display_col_index_list, data):
        """
        普遍性检验报告
        Args:
            data (_type_): _description_
        """
        # print(f'parserMainContexts---{data}')

        # 修正项目中每一行中参杂的无意义的符号
        fixed_data = fixedLineList(data)

        # 将每一行的数据统计在一起,获取最大行
        all_line_data_list = self.comonMainInfo.getMainLineData(fixed_data)

        # 每一行数据中，每项末尾坐标X与相邻参数数据坐标太近，默认是同一个参数
        max_len, fixed_all_line_data_list = self.comonMainInfo.fixedLinePosxData(all_line_data_list)

        # 根据x轴的大小对每一行数据进行排序，防止一行中的数据乱排序
        sortx_line_data = sortedLineData(fixed_all_line_data_list)

        # 随机抽查最大行的数据
        max_line_posx_list = getMaxLineList(max_len, sortx_line_data)

        # 对项目每一行的数据进行查缺补漏
        mainLines = self.comonMainInfo.getMainLineList(max_len, max_line_posx_list, sortx_line_data)

        # 过滤需要展示的列的列表
        mainContextsList = self.comonMainInfo.filterMainDisplayEntrys(display_col_index_list, mainLines)
        # print(f'self.main_title_indx_list---{self.main_title_indx_list}')
        # print(f'mainContextsList---{mainContextsList}')

        # 将无标题的行去掉
        mainEffectiveContextsList = self.comonMainInfo.filterEffectiveLines(mainContextsList)

        # 根据每行数据，得出检验结果
        mainEntrysContexts = self.comonMainInfo.mainResultEntrys(mainEffectiveContextsList)
        # print(f'mainEntrysContexts---{mainEntrysContexts}')
        return mainEntrysContexts

    # def parserLines(self, data: list):
    #     """
    #     获取最大的有效行
    #     最大有效行的所有元素的x坐标
    #     每行进行纠正后的排序
    #     Args:
    #         data (list): _description_
    #     """
    #     max_len = 0  # 最大长度
    #     line_data_list = []  # 含坐标数据的集合
    #     line_data = []  # 每行含坐标数据
    #     compare_y = ''

    #     for indx, item in enumerate(data):
    #         x, y = item[0][0]
    #         text = item[1][0]
    #         if indx == 0:
    #             compare_y = y
    #             line_data.append(item)
    #         else:
    #             if abs(compare_y-y) <= 13:  # 保证在同一行
    #                 line_data.append(item)
    #             else:
    #                 if max_len < len(line_data):
    #                     max_len = len(line_data)

    #                 line_data_list.append(line_data)
    #                 compare_y = y
    #                 line_data = []
    #                 line_data.append(item)

    #     # 根据y轴的大小进行排序
    #     sort_x_data = sortedData(line_data_list)

    #     # 随机抽查最大行的数据
    #     max_line_items = []
    #     for index, line_list in enumerate(sort_x_data):
    #         if max_len == len(line_list):
    #             for line in line_list:
    #                 x, y = line[0][0]
    #                 max_line_items.append(x)
    #             break

    #     return max_len, max_line_items, sort_x_data

    # def getMainLineList(self, data: list, max_len: int, item_pox_list: list):
    #     """
    #     对项目每一行的数据进行查缺补漏
    #     Args:
    #         data (list): _description_
    #     """
    #     mainLines = list()

    #     for indx, line_data in enumerate(data):
    #         entry_data = list()
    #         temp_index = 0
    #         insert_indx_list = []  # 需要插入位置的列表
    #         if len(line_data) != max_len:
    #             for index, item_pox in enumerate(item_pox_list):
    #                 temp_posx, temp_posy = line_data[temp_index][0][0]
    #                 if abs(temp_posx - item_pox) > 38:
    #                     insert_indx_list.append(index)
    #                 else:
    #                     temp_index += 1
    #         # 获取纯文本的一行数据
    #         for line in line_data:
    #             text = line[1][0]
    #             entry_data.append(text)

    #         # 数据补漏
    #         if len(entry_data) != max_len:
    #             for insert_indx in insert_indx_list:
    #                 if insert_indx == 0:
    #                     entry_data.insert(insert_indx, '0')
    #                 else:
    #                     entry_data.insert(insert_indx, '')
    #             mainLines.append(entry_data)
    #         else:
    #             mainLines.append(entry_data)

    #     return mainLines

    # def getMainEntrys(self, main_title_indx_list: list, mainLines: list):
    #     """
    #     获取
    #     Args:
    #         data (list): _description_
    #     """
    #     mainContextsList = list()
    #     for indx, item in enumerate(mainLines):
    #         # print(f'item---{item}')
    #         try:
    #             mainEntrysList = list()
    #             for main_title_indx in main_title_indx_list:
    #                 mainEntrysList.append(item[main_title_indx])
    #             mainContextsList.append(mainEntrysList)
    #         except:
    #             print(traceback.print_exc())

    #     return mainContextsList

    # def mainResultEntrys(self, data: list):
    #     """_summary_
    #     """
    #     for item in data:
    #         try:
    #             # 检验值
    #             res_val_list = re.findall(r'[0-9]+\.*[0-9]*', item[1])
    #             temp_res_val_list = [x for x in res_val_list if x != '0']
    #             res_val = ''.join(temp_res_val_list)
    #         except:
    #             res_val = float(0)
    #         finally:
    #             if res_val == float(0):
    #                 item.append('')
    #                 continue

    #         reference_val = item[3]  # 参考值
    #         if not reference_val:
    #             item.append('')
    #             continue
    #         # 有值的情况
    #         if '-' in reference_val:
    #             min_val, max_val = reference_val.split('-')
    #             if float(res_val) >= float(min_val) and float(res_val) <= float(max_val):
    #                 item.append('正常')
    #             else:
    #                 if float(res_val) < float(min_val):
    #                     item.append('偏低')
    #                 elif float(res_val) > float(max_val):
    #                     item.append('偏高')
    #         else:
    #             reference_val_list = re.findall(r'[0-9]+\.*[0-9]*', item[3])
    #             temp_reference_val_list = [x for x in reference_val_list if x != '0']
    #             reference_val = ''.join(temp_reference_val_list)
    #             if '<' in reference_val:
    #                 if float(res_val) > float(reference_val):
    #                     item.append('偏高')
    #                 else:
    #                     item.append('正常')
    #             elif '>' in reference_val:
    #                 if float(res_val) < float(reference_val):
    #                     item.append('偏低')
    #                 else:
    #                     item.append('正常')
    #             else:
    #                 item.append('')

    #     return data
